﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DontForgetISaw
{
    public partial class ContentForm : Form
    {
        public ContentForm()
        {
            InitializeComponent();
        }

        private string oldTitle = string.Empty;
        private string oldContent = string.Empty;

        private FileInfo file;
        public FileInfo File
        {
            get => file;
            set
            {
                this.file = value;
                readContentFromFile();
            }
        }

        private void readContentFromFile()
        {
            this.Text = this.file.FullName;
            if (!this.file.Exists)
            {
                return;
            }
            using (StreamReader streamReader = new StreamReader(this.file.OpenRead(), Encoding.UTF8))
            {
                string title = streamReader.ReadLine();
                string content = streamReader.ReadToEnd();
                oldTitle = title;
                oldContent = content;
                this.tbTitle.Text = title;
                this.rtbContent.Text = content;
            }
        }

        private void writeContentToFile()
        {
            using (StreamWriter writer = new StreamWriter(this.file.OpenWrite(), Encoding.UTF8))
            {
                string title = this.tbTitle.Text;
                string content = this.rtbContent.Text;
                writer.Write(title);
                writer.WriteLine();
                writer.Write(content);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (needSave())
            {
                this.writeContentToFile();
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!needSave())
            {
                this.Close();
                return;
            }

            DialogResult dialogResult = MessageBox.Show("当前内容已经修改，是否保存?", "有未保存的内容", MessageBoxButtons.YesNoCancel);
            if (dialogResult == DialogResult.Yes)
            {
                this.writeContentToFile();
                this.Close();
            }
            else if (dialogResult == DialogResult.No)
            {
                this.Close();
            }
        }

        private bool needSave()
        {
            return !string.Equals(this.tbTitle.Text, oldTitle)
               || !string.Equals(this.rtbContent.Text, oldContent);
        }

        private void ContentForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                if (this.needSave())
                {
                    this.writeContentToFile();
                    this.lbStatus.Text = this.file.Name + "保存成功.";
                }
            }
        }

        private void rtbContent_TextChanged(object sender, EventArgs e)
        {
            this.lbStatus.Text = string.Empty;
        }

        private void tbTitle_TextChanged(object sender, EventArgs e)
        {
            this.lbStatus.Text = string.Empty;
        }
    }
}
